﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace TimeRuler
{
    /// <summary>
    /// TimerRunner.xaml 的交互逻辑
    /// </summary>
    public partial class TimerRunner : Page
    {
        DispatcherTimer timeTimer = new();
        private int tempTime = 0;
        private int goalTime = 0;
        public TimerRunner()
        {
            InitializeComponent();
            //计时器设置
            timeTimer.Tick += TimeTimer_Tick;
            timeTimer.Interval = new TimeSpan(TimeSpan.TicksPerSecond);
        }

        private void TimeTimer_Tick(object? sender, EventArgs e)
        {
            tempTime++;
            Time.TotalTime++;
            if (tempTime>=goalTime&&goalTime!=0)
            {
                timeTimer.Stop();
                MessageBox.Show("目标达成！即将返回主界面。");
                NavigationService.GoBack();
                return;
            }
            RefreshWindow();
        }
        private void startTimeRecordButton_Click(object sender, RoutedEventArgs e)
        {
            if (!timeTimer.IsEnabled)
            {
                timeTimer.Start();
                timeGoalBox.IsEnabled = false;
                goalTime = Convert.ToInt32(timeGoalBox.Text) * 60;
                startTimeRecordButton.Content = "保存并停止";
            }
            else
            {
                timeTimer.Stop();
                tempTime = 0;
                startTimeRecordButton.Content = "开始";
                NavigationService.GoBack();
            }
        }
        private void RefreshWindow()
        {
            //计时器
            timeRecordLabel.Content = TimeConvert.DescribeTime(tempTime, "已计时");
            //进度条
            if (goalTime != 0)
                timeProgressBar.Value = Convert.ToDouble(tempTime) / Convert.ToDouble(goalTime) * 100;
        }
    }
}
